package com.ibks.iframe;

import com.ibks.iframe.context.ApplicationContextHelper;
import com.ibks.iframe.context.PersistenceContext;
import com.ibks.iframe.persistence.PersistenceException;
import com.ibks.iframe.persistence.impl.iquery.PersistenceLayerImpl;
import com.ibksystem.qm.run.QueryManagerSession;

public class DBHelper
{
    private static DBHelper dbhelper;

    private PersistenceLayerImpl persistenceLayer;

    private DBHelper()
    {
        PersistenceContext pctx = (PersistenceContext) ApplicationContextHelper.getApplicationContext().getSubContext(
                PersistenceContext.NAME);
        this.persistenceLayer = (PersistenceLayerImpl) pctx.getPersistenceLayer();
    }

    private static final DBHelper getInstance()
    {
        if (dbhelper == null)
        {
            dbhelper = new DBHelper();
        }
        return dbhelper;
    }

    public static QueryManagerSession getSession() throws PersistenceException
    {
        return getInstance().persistenceLayer.getSession();
    }

    public static void closeSession() throws PersistenceException
    {
        getInstance().persistenceLayer.closeSession();
    }

    public static QueryManagerSession getNewSession() throws PersistenceException
    {
        return getInstance().persistenceLayer.getNewSession();
    }

    public static QueryManagerSession getNewSession(String configName) throws PersistenceException
    {
        return getInstance().persistenceLayer.getNewSession(configName);
    }
}
